
package com.epam.androids.training.day11;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.io.IOException;

/**
 * Player activity
 * 
 * @author Natallia_Kukharchyk
 */
public class MainActivity extends Activity {
  private MediaPlayer resourcePlayer;

  private Button start;

  private Button next;

  private Button stop;

  private Button back;

  private static String TAG = "mediaPlayer";

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    resourcePlayer = MediaPlayer.create(getApplicationContext(), R.raw.twin);

    start = (Button) findViewById(R.id.btn_start);
    start.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        if (resourcePlayer.isPlaying()) {
          resourcePlayer.pause();
          start.setText(R.string.start);
        } else {
          resourcePlayer.start();
          start.setText(R.string.pause);

        }
      }
    });

    next = (Button) findViewById(R.id.btn_next);
    next.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        // just switch for another track from raw resources
        changeMelody(R.raw.vivaldi);
      }
    });

    back = (Button) findViewById(R.id.btn_back);
    back.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        changeMelody(R.raw.twin);
      }
    });

    stop = (Button) findViewById(R.id.btn_stop);
    stop.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        resourcePlayer.stop();
        start.setText(R.string.start);
        try {
          resourcePlayer.prepare();
        } catch (IOException ex) {
          Log.e(TAG, ex.toString());
        }
      }
    });
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    resourcePlayer.release();

  }

  private boolean changeMelody(int resId) {
    // when user click next or back stop current track and setup device for
    // other one (set datasource and prepare)
    resourcePlayer.stop();
    resourcePlayer.reset();
    AssetFileDescriptor afd = getApplicationContext().getResources().openRawResourceFd(resId);
    if (afd == null) {
      return false;
    }

    try {
      resourcePlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
      afd.close();
      resourcePlayer.prepare();
    } catch (IllegalArgumentException e) {
      Log.e(TAG, e.toString());
      return false;
    } catch (IllegalStateException e) {
      Log.e(TAG, e.toString());
      return false;
    } catch (IOException e) {
      Log.e(TAG, e.toString());
      return false;
    }
    resourcePlayer.start();
    return true;
  }
}
